package com.example.keepmesafe.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.keepmesafe.R;
import com.example.keepmesafe.activities.UserHome;
import com.example.keepmesafe.dialogs.CustomOptionsDialog;
import com.example.keepmesafe.dialogs.ShareTagDialog;
import com.example.keepmesafe.domain.Tag;
import com.example.keepmesafe.service.UtilService;
import com.example.keepmesafe.service.impl.UtilServiceImpl;

public class InitialiseTagsAdapter extends ArrayAdapter<Tag> {
	private final Context context;
    private final ArrayList<Tag> itemsArrayList;
    
    private final Activity parentActivity;
    
	public InitialiseTagsAdapter(Context context, ArrayList<Tag> itemsArrayList,Activity parentActivity) {
		super(context,R.layout.row,itemsArrayList);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.itemsArrayList = itemsArrayList;
		this.parentActivity = parentActivity;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 1. Create inflater 
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 2. Get rowView from inflater
        View rowView = inflater.inflate(R.layout.row, parent, false);

        // 3. Get the three text view from the rowView
        TextView labelTagName = (TextView) rowView.findViewById(R.id.labelTagName);
        TextView valueTagUsername = (TextView) rowView.findViewById(R.id.valueTagUsername);
        TextView valueTagPassword = (TextView) rowView.findViewById(R.id.valueTagPassword);
        ImageView shareIcon = (ImageView)rowView.findViewById(R.id.iconShare);
        
        // 4. Set the text for textView 
        labelTagName.setText(itemsArrayList.get(position).getTagName());
        valueTagUsername.setText(itemsArrayList.get(position).getTagUsername());
        valueTagPassword.setText(itemsArrayList.get(position).getTagPassword());

        Tag tag = new Tag();
        tag.setId(itemsArrayList.get(position).getId());
        tag.setTagName(itemsArrayList.get(position).getTagName());
        tag.setTagUsername(itemsArrayList.get(position).getTagUsername());
        tag.setTagPassword(itemsArrayList.get(position).getTagPassword());
        
        final DialogFragment customOptionsDialog = new CustomOptionsDialog(parentActivity,context,tag);
        final DialogFragment shareTagDialog = new ShareTagDialog(parentActivity,context, tag);
        final FragmentManager f = parentActivity.getFragmentManager();
        
        try {
			 // 5. Set onclick listener for row to show options 
	        rowView.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					// TODO Auto-generated method stub
					customOptionsDialog.show(f, "options tag dialog");
					return false;
				}
			});
	        
	        shareIcon.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					shareTagDialog.show(f, "share tag dialog");
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        
        // 6. return rowView
        return rowView;
    }
}
